#include "wangge.h"
#include "ui_wangge.h"
#include "QPainter"
#include "math.h"
int num=0;
wangge::wangge(QWidget *parent) :
    QMainWindow(parent)
{
    resize(1000,500);
    time = new QLabel(this);
    time->resize(100,100);
    time->move(0,600);
    int i=0;
    for(i=0;i<21;i++)
    {
        x[i]=20*i;
        y[i]=400;
    }
    for(i=0;i<21;i++)
    {
        x[i]=20*i;
        z[i]=400;
    }
    cputext = new QLabel(this);
    memtext = new QLabel(this);
    cputext->setText("CPU");
    cputext->move(710,35);
    memtext->move(710,85);
    memtext->setText("MEM");
}

wangge::~wangge()
{
}

void wangge::timerEvent(QTimerEvent *)
{

    this->repaint();

}
void wangge::paintEvent(QPaintEvent *)
{
    int i=0,j=0;
    QPainter painter(this);
    QPen pen(Qt::SolidLine);

    painter.setPen(pen);
    painter.drawLine(0,0,0,400);
    painter.drawLine(0,400,400,400);
    pen.setStyle(Qt::SolidLine);
    pen.setStyle(Qt::DotLine);
    pen.setColor(QColor(255, 0, 0, 127));
    painter.setPen(pen);
    for(i=40;i<=400;i+=40)
    {
        painter.drawLine(0,i,400,i);
    }
    for(i=40;i<=400;i+=40)
    {
        painter.drawLine(i,0,i,400);
    }
    pen.setStyle(Qt::SolidLine);
    pen.setColor(QColor(255,34,10,255));
    pen.setWidth(4);
    painter.setPen(pen);

    for(i=0;i<20;i++)
    {
        painter.drawLine(x[i],y[i],x[i+1],y[i+1]);
    }
    pen.setStyle(Qt::SolidLine);
    pen.setColor(QColor(10,250,10,250));
    pen.setWidth(4);
    painter.setPen(pen);

    for(i=0;i<20;i++)
    {
        painter.drawLine(x[i],z[i],x[i+1],z[i+1]);
    }

    pen.setStyle(Qt::SolidLine);
    pen.setColor(QColor(255,34,10,255));
    pen.setWidth(4);
    painter.setPen(pen);
    painter.drawLine(500,50,700,50);
    pen.setStyle(Qt::SolidLine);
    pen.setColor(QColor(10,250,10,250));
    pen.setWidth(4);
    painter.setPen(pen);
    painter.drawLine(500,100,700,100);
}
    void wangge::set_cpu(QString s)
    {
        QStringList allinfo=s.split(" ");//ַտոָallinfoַȥ
        this->time->setText(s);
        int i=0;
        if(1)
        {
            //num%2==0
            for(i=0;i<20;i++)
            {
                y[i]=y[i+1];
                z[i]=z[i+1];
            }
            //QString a=s;
            //int per = a.toInt();

            y[20]=400-4*(allinfo[0].toInt());
            z[20]=400-4*(allinfo[1].toInt());
            num++;
        }
        else
        {
            for(i=0;i<20;i++)
            {
                z[i]=z[i+1];
            }
            QString a=s;
            int per = a.toInt();

            z[20]=400-4*per;
            num++;
        }
        this->update();

    }


